package pATT.core.visual;

import java.awt.Color;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.JWindow;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.tree.DefaultMutableTreeNode;

import pATT.DataSetsGenerator.dataSetsGenerator.core.visual.RelationGenerator.ToDoListModel;
import pATT.core.Attribute;
import pATT.core.AttributeStats;
import pATT.core.InstanceManager;
import pATT.core.Instances;
import pATT.core.Utils;
import pATT.core.Chooser.ImageFilter;
import pATT.filters.Filter;
import pATT.logger.Logger;
import pATT.logger.Message;
import pATT.viewer.EditorView;

public class PreProcessPanel extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * variable graficas 
	 */
	private JPanel jPreProcessPanel = null;
	private JButton jOpenFileButton = null;
	private JButton jEditButton = null;
	private JButton jSaveButton = null;
	private JButton jChooseButton = null;
	private JTextField jFilterTextField = null;
	private JButton jApplyButton = null;
	private JPanel jRealtionPanel = null;
	private JPanel jPanel = null;
	private JPanel jPanel1 = null;
	private JScrollPane jChooseScrollPane1 = null;
	private JPanel jFilterPanel2 = null;
	private JPanel jPanel2 = null;
	private JList jFilterAddedList = null;
	private JButton jFilterRemoveButton4 = null;
	private JTree jFilterTree1 = null;
	private JWindow jFilterWindow = null;
	private JLabel jRelationNameLabel2 = null;
	private JLabel jRelationNameLabel = null;
	private JLabel jInstancesLabel2 = null;
	private JLabel jInstancesLabel = null;
	private JLabel jAttributesLabel2 = null;
	private JLabel jAttributesLabel = null;
	private JScrollPane jScrollPane7 = null;
	private JTable jAttributesTable = null;
	private JLabel jAttributeNameLabel = null;
	private JLabel jAttributeNameLabel1 = null;
	private JLabel jMissingLabel2 = null;
	private JLabel jMissingLabel = null;
	private JLabel jDistinctLabel2 = null;
	private JLabel jDistinctLabel = null;
	private JLabel jUniqueLabel2 = null;
	private JLabel jUniqueLabel = null;
	private JButton jApplyFiltersButton = null;
	
	/** Contador para la tabla de atributos del archivo arff seleccionado*/	
	private int contTable;
	
	/** Un thread en el que correra el algoritmo */
	protected Thread m_RunThread;
	
	/** The file chooser for selecting arff files */
	protected GenericObjectEditor m_FilterEditor = new GenericObjectEditor();
	private JScrollPane jFilterAddedScrollPane = null;
	
	public PreProcessPanel() {
		super();
		initialize();
	}
	
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		
		contTable = 0;
	}
	
	
	
	/**
	 * This method initializes jPreProcessPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	public JPanel getJPreProcessPanel() {
		if (jPreProcessPanel == null) {
			jPreProcessPanel = new JPanel();
			jPreProcessPanel.setLayout(null);
			jPreProcessPanel.setBackground(java.awt.SystemColor.control);
			jPreProcessPanel.add(getJOpenFileButton(), null);
			jPreProcessPanel.add(getJEditButton(), null);
			jPreProcessPanel.add(getJSaveButton(), null);
			jPreProcessPanel.add(getJRealtionPanel(), null);
			jPreProcessPanel.add(getJPanel(), null);
			jPreProcessPanel.add(getJPanel1(), null);
			jPreProcessPanel.add(getJFilterPanel2(), null);
			jPreProcessPanel.add(getJPanel2(), null);
			jPreProcessPanel.add(getJApplyFiltersButton(), null);
		}
		return jPreProcessPanel;
	}
	
	/**
	 * This method initializes jOpenFileButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJOpenFileButton() {
		if (jOpenFileButton == null) {
			jOpenFileButton = new JButton();
			jOpenFileButton.setText("Open File");
			jOpenFileButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jOpenFileButton.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jOpenFileButton.setLocation(new java.awt.Point(25,10));
			jOpenFileButton.setSize(new java.awt.Dimension(145,28));
			jOpenFileButton.setIcon(new ImageIcon(getClass().getResource("/pATT/icon/Open24.gif")));
			jOpenFileButton.setBackground(java.awt.SystemColor.control);
			jOpenFileButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					fc.setBackground(java.awt.SystemColor.control);
					fc.setAcceptAllFileFilterUsed(false); //TODO
					ImageFilter ifa = new ImageFilter(ImageFilter.ARFF_EXTENSION);
					ImageFilter ifa1 = new ImageFilter(ImageFilter.PERSONAL_SEARCHER_EXTENSION);
					ImageFilter ifa2 = new ImageFilter(ImageFilter.SAVER_EXTENSION);
					
					fc.addChoosableFileFilter(ifa);
					fc.addChoosableFileFilter(ifa1);
					fc.addChoosableFileFilter(ifa2);
					int returnVal = fc.showOpenDialog(fc);
					
					if (returnVal == JFileChooser.APPROVE_OPTION) {			
						cleanJTable();
						clearAttributeInfo();
						clearFitersAdded();
						jSaveButton.setEnabled(false);
						jEditButton.setEnabled(false);
						jFilterRemoveButton4.setEnabled(false);
						jApplyButton.setEnabled(false);
						jChooseButton.setEnabled(false);
						jApplyFiltersButton.setEnabled(false);

						if(ifa.isArffFileValid(fc.getSelectedFile())){
							try {
								InstanceManager.eINSTANCE.createIntancesFromArff(fc.getSelectedFile());
							} catch (IOException e1) {
								 JOptionPane.showMessageDialog(PreProcessPanel.this,
										  Message.ERROR_LOADER_FILE
										  + fc.getSelectedFile().getName(),
										  Message.ERROR_LOADER_FILE_TITLE,
										  JOptionPane.ERROR_MESSAGE);

							}
							jSaveButton.setEnabled(true);
							jEditButton.setEnabled(true);
							jFilterRemoveButton4.setEnabled(true);
							jApplyButton.setEnabled(true);
							jChooseButton.setEnabled(true);
							jApplyFiltersButton.setEnabled(false);
							
							UserProfilingUI.eINSTANCE.OpenFilePreProcess(true);
						}else if(ifa.isFileValid(fc.getSelectedFile(), ImageFilter.PERSONAL_SEARCHER_EXTENSION)){
							//TODO si ingreso un archivo de entrada para el Personal Searcher ".psearcher"
							File file = fc.getSelectedFile();
							Logger.log(Message.OPEN_OTHER_FILE+file.getName());
							InstanceManager.eINSTANCE.setFileLoaded(fc.getSelectedFile());
							new OtherFileDialog(file.toString(), Utils.fileToString(file));
							
							UserProfilingUI.eINSTANCE.OpenFilePreProcess(true);
//							updateRelationInfo("None",-1,-1);
						}else if(ifa.isFileValid(fc.getSelectedFile(), ImageFilter.SAVER_EXTENSION)){
							//TODO si ingreso un archivo de entrada para el SAVER ".xmlsaver"
							File file = fc.getSelectedFile();
							Logger.log(Message.OPEN_OTHER_FILE+file.getName());
							InstanceManager.eINSTANCE.setFileLoaded(fc.getSelectedFile());
							new OtherFileDialog(file.toString(), Utils.fileToString(file));
							
							UserProfilingUI.eINSTANCE.OpenFilePreProcess(true);
//							updateRelationInfo("None",-1,-1);
						}else{
							jSaveButton.setEnabled(false);
							jEditButton.setEnabled(false);
							jFilterRemoveButton4.setEnabled(false);
							jApplyButton.setEnabled(false);
							jApplyFiltersButton.setEnabled(false);
							jChooseButton.setEnabled(false);
							UserProfilingUI.eINSTANCE.OpenFilePreProcess(false);
							updateRelationInfo("None",-1,-1);
							Logger.log(Message.OPEN_FILE_ERROR);
							
						}
					}
					
				}
			});
		}
		return jOpenFileButton;
	}
	
	/**
	 * This method initializes jEditButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJEditButton() {
		if (jEditButton == null) {
			jEditButton = new JButton();
			jEditButton.setText("Edit");
			jEditButton.setEnabled(false);
			jEditButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jEditButton.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jEditButton.setLocation(new java.awt.Point(231,10));
			jEditButton.setSize(new java.awt.Dimension(145,28));
			jEditButton.setIcon(new ImageIcon(getClass().getResource("/pATT/icon/preferences.gif")));
			jEditButton.setBackground(java.awt.SystemColor.control);
			jEditButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					new EditorView().show();
					
				}
			});
		}
		return jEditButton;
	}
	
	/**
	 * This method initializes jSaveButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJSaveButton() {
		if (jSaveButton == null) {
			jSaveButton = new JButton();
			jSaveButton.setEnabled(false);
			jSaveButton.setText("Save");
			jSaveButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jSaveButton.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jSaveButton.setLocation(new java.awt.Point(427,10));
			jSaveButton.setSize(new java.awt.Dimension(145,28));
			jSaveButton.setIcon(new ImageIcon(getClass().getResource("/pATT/icon/Save24.gif")));
			jSaveButton.setBackground(java.awt.SystemColor.control);
			jSaveButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					fc.setBackground(java.awt.SystemColor.control);
					ImageFilter ifa=new ImageFilter(ImageFilter.ARFF_EXTENSION); 
					fc.addChoosableFileFilter(ifa);
					int returnVal = fc.showDialog(fc,"Save");
					
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						
						try {
							
							File file = new File(fc.getSelectedFile().getPath()+".arff");
							BufferedWriter bw = new BufferedWriter(new FileWriter(file));								
							bw.write(InstanceManager.eINSTANCE.getArffFile());
							
							bw.close();
							Logger.log(Message.SAVE_FILE+InstanceManager.eINSTANCE.nameRelation());
						} catch (FileNotFoundException e1) {
							e1.printStackTrace();
						} catch (IOException e1) {
							e1.printStackTrace();
						}
					}
					
				}
			});
		}
		return jSaveButton;
	}
	
	/**
	 * This method initializes jRealtionPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJRealtionPanel() {
		if (jRealtionPanel == null) {
			jAttributesLabel = new JLabel();
			jAttributesLabel.setText("");
			jAttributesLabel.setSize(new java.awt.Dimension(86,23));
			jAttributesLabel.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jAttributesLabel.setLocation(new java.awt.Point(282,50));
			jAttributesLabel2 = new JLabel();
			jAttributesLabel2.setText("Attributes: ");
			jAttributesLabel2.setSize(new java.awt.Dimension(58,23));
			jAttributesLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jAttributesLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
			jAttributesLabel2.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jAttributesLabel2.setLocation(new java.awt.Point(224,50));
			jInstancesLabel = new JLabel();
			jInstancesLabel.setText("");
			jInstancesLabel.setLocation(new java.awt.Point(72,50));
			jInstancesLabel.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jInstancesLabel.setSize(new java.awt.Dimension(78,23));
			jInstancesLabel2 = new JLabel();
			jInstancesLabel2.setPreferredSize(new java.awt.Dimension(39,14));
			jInstancesLabel2.setLocation(new java.awt.Point(14,50));
			jInstancesLabel2.setSize(new java.awt.Dimension(58,23));
			jInstancesLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jInstancesLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
			jInstancesLabel2.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jInstancesLabel2.setText("Instances: ");
			jRelationNameLabel = new JLabel();
			jRelationNameLabel.setBounds(new java.awt.Rectangle(68,17,307,23));
			jRelationNameLabel.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jRelationNameLabel.setText("");
			jRelationNameLabel2 = new JLabel();
			jRelationNameLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jRelationNameLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
			jRelationNameLabel2.setSize(new java.awt.Dimension(58,23));
			jRelationNameLabel2.setLocation(new java.awt.Point(10,17));
			jRelationNameLabel2.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jRelationNameLabel2.setText("Relation:");
			jRealtionPanel = new JPanel();
			jRealtionPanel.setLayout(null);
			jRealtionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.blue,1), "Current Relation", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11), java.awt.Color.blue));
			jRealtionPanel.setBounds(new java.awt.Rectangle(6,113,383,80));
			jRealtionPanel.setBackground(java.awt.SystemColor.control);
			jRealtionPanel.add(jRelationNameLabel2, null);
			jRealtionPanel.add(jRelationNameLabel, null);
			jRealtionPanel.add(jInstancesLabel2, null);
			jRealtionPanel.add(jInstancesLabel, null);
			jRealtionPanel.add(jAttributesLabel2, null);
			jRealtionPanel.add(jAttributesLabel, null);
		}
		return jRealtionPanel;
	}
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.blue,1), "Attributes", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11), java.awt.Color.blue));
			jPanel.setBounds(new java.awt.Rectangle(6,197,383,192));
			jPanel.setBackground(java.awt.SystemColor.control);
			jPanel.add(getJScrollPane7(), null);
		}
		return jPanel;
	}
	
	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jUniqueLabel = new JLabel();
			jUniqueLabel.setText("");
			jUniqueLabel.setSize(new java.awt.Dimension(51,23));
			jUniqueLabel.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jUniqueLabel.setLocation(new java.awt.Point(318,50));
			jUniqueLabel2 = new JLabel();
			jUniqueLabel2.setText("Unique: ");
			jUniqueLabel2.setSize(new java.awt.Dimension(58,23));
			jUniqueLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jUniqueLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
			jUniqueLabel2.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jUniqueLabel2.setLocation(new java.awt.Point(260,50));
			jDistinctLabel = new JLabel();
			jDistinctLabel.setText("");
			jDistinctLabel.setSize(new java.awt.Dimension(51,23));
			jDistinctLabel.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jDistinctLabel.setLocation(new java.awt.Point(190,50));
			jDistinctLabel2 = new JLabel();
			jDistinctLabel2.setText("Distinct: ");
			jDistinctLabel2.setSize(new java.awt.Dimension(58,23));
			jDistinctLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jDistinctLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
			jDistinctLabel2.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jDistinctLabel2.setLocation(new java.awt.Point(132,50));
			jMissingLabel = new JLabel();
			jMissingLabel.setText("");
			jMissingLabel.setLocation(new java.awt.Point(68,50));
			jMissingLabel.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jMissingLabel.setSize(new java.awt.Dimension(51,23));
			jMissingLabel2 = new JLabel();
			jMissingLabel2.setText("Missing: ");
			jMissingLabel2.setSize(new java.awt.Dimension(58,23));
			jMissingLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jMissingLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
			jMissingLabel2.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jMissingLabel2.setLocation(new java.awt.Point(10,50));
			jAttributeNameLabel1 = new JLabel();
			jAttributeNameLabel1.setText("");
			jAttributeNameLabel1.setLocation(new java.awt.Point(67,16));
			jAttributeNameLabel1.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jAttributeNameLabel1.setSize(new java.awt.Dimension(258,23));
			jAttributeNameLabel = new JLabel();
			jAttributeNameLabel.setText("Name: ");
			jAttributeNameLabel.setSize(new java.awt.Dimension(58,23));
			jAttributeNameLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jAttributeNameLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
			jAttributeNameLabel.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jAttributeNameLabel.setLocation(new java.awt.Point(10,16));
			jPanel1 = new JPanel();
			jPanel1.setLayout(null);
			jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.blue,1), "Selected Attribute Info", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11), java.awt.Color.blue));
			jPanel1.setBounds(new java.awt.Rectangle(396,309,383,80));
			jPanel1.setBackground(java.awt.SystemColor.control);
			jPanel1.add(jAttributeNameLabel, null);
			jPanel1.add(jAttributeNameLabel1, null);
			jPanel1.add(jMissingLabel2, null);
			jPanel1.add(jMissingLabel, null);
			jPanel1.add(jDistinctLabel2, null);
			jPanel1.add(jDistinctLabel, null);
			jPanel1.add(jUniqueLabel2, null);
			jPanel1.add(jUniqueLabel, null);
		}
		return jPanel1;
	}
	/**
	 * This method initializes jFilterPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJFilterPanel2() {
		if (jFilterPanel2 == null) {
			jFilterPanel2 = new JPanel();
			jFilterPanel2.setLayout(null);
			jFilterPanel2.setBounds(new java.awt.Rectangle(6,49,774,56));
			jFilterPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.blue,1), "Filter", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11), java.awt.Color.blue));
			jFilterPanel2.setBackground(java.awt.SystemColor.control);
			jFilterPanel2.add(getJChooseButton(), null);
			jFilterPanel2.add(getJFilterTextField(), null);
			jFilterPanel2.add(getJApplyButton(), null);
		}
		return jFilterPanel2;
	}
	
	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jPanel2 = new JPanel();
			jPanel2.setLayout(null);
			jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.blue,1), "Filters added", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11), java.awt.Color.blue));
			jPanel2.setBackground(java.awt.SystemColor.control);
			jPanel2.setBounds(new java.awt.Rectangle(396,114,383,190));
			jPanel2.add(getJFilterRemoveButton4(), null);
			jPanel2.add(getJFilterAddedScrollPane(), null);
		}
		return jPanel2;
	}
	/**
	 * This method initializes jApplyFiltersButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJApplyFiltersButton() {
		if (jApplyFiltersButton == null) {
			jApplyFiltersButton = new JButton();
			jApplyFiltersButton.setText("Apply Filters");
			jApplyFiltersButton.setEnabled(false);
			jApplyFiltersButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jApplyFiltersButton.setSize(new java.awt.Dimension(145,28));
			jApplyFiltersButton.setBackground(java.awt.SystemColor.control);
			jApplyFiltersButton.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jApplyFiltersButton.setIcon(new ImageIcon(getClass().getResource("/pATT/icon/zoomin.gif")));
			jApplyFiltersButton.setLocation(new java.awt.Point(616,10));
			jApplyFiltersButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						InstanceManager.eINSTANCE.applyFilters();
//						TODO borrar los filtros de la lista
						clearFitersAdded();				
						jApplyFiltersButton.setEnabled(false);
					} catch (Exception e1) {
						InstanceManager.eINSTANCE.recuperarInstances();
						 JOptionPane.showMessageDialog(PreProcessPanel.this,
								  Message.ERROR_FILTER
								  + e1.getMessage(),
								  Message.ERROR_FILTER_TILTLE,
								  JOptionPane.ERROR_MESSAGE);
					}
					
					
				}
			});
		}
		return jApplyFiltersButton;
	}
	
	/**
	 * This method initializes jScrollPane7	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane7() {
		if (jScrollPane7 == null) {
			jScrollPane7 = new JScrollPane();
			jScrollPane7.setBounds(new java.awt.Rectangle(6,14,370,173));
			jScrollPane7.setViewportView(getJAttributesTable());
		}
		return jScrollPane7;
	}
	
	/**
	 * This method initializes jAttributesTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJAttributesTable() {
		if (jAttributesTable == null) {
			jAttributesTable = new JTable();
			TableColumn tc1= new TableColumn();
			TableColumn tc2= new TableColumn();
			tc1.setPreferredWidth(10); 
			tc1.setHeaderValue("Number");
			tc2.setHeaderValue("Name");
			jAttributesTable.addColumn(tc1);
			jAttributesTable.addColumn(tc2);// JVE Generated
			tc1.setModelIndex(0);
			tc2.setModelIndex(1);
			jAttributesTable.setAutoCreateColumnsFromModel(false); // JVE Generated
			jAttributesTable.setModel(new ToDoListModel());
			jAttributesTable.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 10));
			jAttributesTable.addMouseListener(new java.awt.event.MouseListener() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					int row = jAttributesTable.getSelectedRow();

					if(!InstanceManager.eINSTANCE.hasInstances() && InstanceManager.eINSTANCE.hasFile()){
						if(contTable > row){
							String att = jAttributesTable.getValueAt(row,1).toString();
							updateAttributeInfo(att);
						}
					}else{	
						if((contTable > 0) && (row < InstanceManager.eINSTANCE.numAttributes())){
							Attribute attribute = InstanceManager.eINSTANCE.getAttribute(row);
							AttributeStats stats = InstanceManager.eINSTANCE.getAttributeStats(row);
							updateAttributeInfo(attribute,stats);
						}
					}
				}
				public void mousePressed(java.awt.event.MouseEvent e) {
				}
				public void mouseReleased(java.awt.event.MouseEvent e) {
				}
				public void mouseEntered(java.awt.event.MouseEvent e) {
				}
				public void mouseExited(java.awt.event.MouseEvent e) {
				}
			});
			
			
		}
		return jAttributesTable;
	}
	
	/**
	 * This method initializes jChooseButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJChooseButton() {
		if (jChooseButton == null) {
			jChooseButton = new JButton();
			jChooseButton.setText(Message.CHOOSE);
			jChooseButton.setEnabled(false);
			jChooseButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jChooseButton.setBackground(java.awt.SystemColor.control);
			jChooseButton.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jChooseButton.setBounds(new java.awt.Rectangle(14,20,121,26));
			jChooseButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setEnabled(false);
					getJChooseScrollPane1().setVisible(true);
					getJFilterTree1().setVisible(true);
					UserProfilingUI.eINSTANCE.setEnabled(false);
					getJFilterWindow().show();
					
				}
			});
		}
		return jChooseButton;
	}
	
	/**
	 * This method initializes jFilterTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJFilterTextField() {
		if (jFilterTextField == null) {
			jFilterTextField = new JTextField();
			jFilterTextField.setEditable(false);
			jFilterTextField.setToolTipText(Message.CLICK_PARAMETERS_FILTER);
			jFilterTextField.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.BOLD, 11));
			jFilterTextField.setBounds(new java.awt.Rectangle(138,20,498,26));
			jFilterTextField.setBackground(Color.WHITE);
			
			jFilterTextField.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if (!jFilterTextField.getText().equals("")){
						//TODO asi se hacia antes
//						Filter filter = InstanceManager.eINSTANCE.getFilter(jFilterTextField.getText());
//						InstanceManager.eINSTANCE.getFilterValues(filter).show();
						m_FilterEditor .setClassType(Filter.class);
						Filter filter = InstanceManager.eINSTANCE.getFilter(jFilterTextField.getText());
						m_FilterEditor.setValue(filter);
						
						m_FilterEditor.showEditor();
						
//						m_BAlgorithmEditor.addPropertyChangeListener(new PropertyChangeListener() {
//						public void propertyChange(PropertyChangeEvent e) {
//						repaint();
//						
//						}
//						});
						
					}
					
				}
			});
		}
		return jFilterTextField;
	}
	
	/**
	 * This method initializes jApplyButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJApplyButton() {
		if (jApplyButton == null) {
			jApplyButton = new JButton();
			jApplyButton.setText("Add Filter");
			jApplyButton.setEnabled(false);
			jApplyButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jApplyButton.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jApplyButton.setBackground(java.awt.SystemColor.control);
			jApplyButton.setBounds(new java.awt.Rectangle(640,20,121,26));
			
			jApplyButton.addActionListener(new java.awt.event.ActionListener() {   
				@SuppressWarnings("unchecked")
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					if (!jFilterTextField.getText().equals("") && !InstanceManager.eINSTANCE.exists(jFilterTextField.getText())){
						//listDataFiltros.add(jFilterTextField.getText());
						
						//TODO aca se guarda el filtro en un vector
						Filter filter = InstanceManager.eINSTANCE.getFilter(jFilterTextField.getText());
						InstanceManager.eINSTANCE.addFilterOfRelation(filter);
						
						jFilterAddedList.setListData(InstanceManager.eINSTANCE.getAddedFilter());
						jFilterTextField.setText("");
						jApplyFiltersButton.setEnabled(true);
					}
				}
				
			});
		}
		return jApplyButton;
	}
	
	/**
	 * This method initializes jFilterAddedList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getJFilterAddedList() {
		if (jFilterAddedList == null) {
			jFilterAddedList = new JList();
			jFilterAddedList.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
		}
		return jFilterAddedList;
	}
	
	/**
	 * This method initializes jFilterRemoveButton4	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJFilterRemoveButton4() {
		if (jFilterRemoveButton4 == null) {
			jFilterRemoveButton4 = new JButton();
			jFilterRemoveButton4.setBounds(new java.awt.Rectangle(10,156,364,28));
			jFilterRemoveButton4.setEnabled(false);
			jFilterRemoveButton4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jFilterRemoveButton4.setMnemonic(java.awt.event.KeyEvent.VK_DELETE);
			jFilterRemoveButton4.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jFilterRemoveButton4.setBackground(java.awt.SystemColor.control);
			jFilterRemoveButton4.setText("Delete Filter");
			jFilterRemoveButton4.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					String nameFiltro=(String) jFilterAddedList.getSelectedValue();
					if ((nameFiltro!=null) && InstanceManager.eINSTANCE.exists(nameFiltro)){
						//listDataFiltros.removeElement(nameFiltro);
						
						Filter filter = InstanceManager.eINSTANCE.getFilter(nameFiltro);
						InstanceManager.eINSTANCE.removeFilterOfRelation(filter);
						jFilterAddedList.setListData(InstanceManager.eINSTANCE.getAddedFilter());
						if(InstanceManager.eINSTANCE.isEmptyAddedFilters()){
							jApplyFiltersButton.setEnabled(false);
						}
						
					}
					
				}
			});
		}
		return jFilterRemoveButton4;
	}
	
	/**
	 * Metodo para inicializar la tabla
	 * */
	private void cleanJTable(){
		contTable = 0;
		//limpiar la tabla
		//jAttributesTable.setModel(new Todo)
		jAttributesTable = null;
		jScrollPane7.setViewportView(getJAttributesTable());
	}
	
	/**
	 * Metodo para visualizar la informacion de los atributos en la pantalla de pre-proceso
	 * 
	 * @param dataSets
	 * 
	 * */
	public void updateAttributesInfo(Instances dataSets){
		cleanJTable();
		clearAttributeInfo();
		Attribute temp = null;
		int max = dataSets.numAttributes();
		int pos = 0;
		TableModel model = jAttributesTable.getModel();
		do {
			temp = dataSets.attribute(pos);
			//ingresa el nombre de los atributos en la tabla
			System.out.println("attribute: "+pos+" "+temp.name());
			//TODO me tiro un error con el calendar
			model.setValueAt(String.valueOf(contTable),contTable,0);
			model.setValueAt(temp.name(),contTable, 1);
			contTable ++;
			pos++;
		} while (pos < max);
		
	}
	/**
	 * Metodo para visualizar la informacion de los atributos en la pantalla de pre-proceso, para el caso de que 
	 * el archivo de entrada no sea un datasets(Arff)
	 * 
	 * @param listAtt
	 * 
	 * */
	public void updateAttributesInfo(Enumeration enumerationAtt){
		cleanJTable();
		clearAttributeInfo();
		TableModel model = jAttributesTable.getModel();
		
		while (enumerationAtt.hasMoreElements()) {
			String att = (String) enumerationAtt.nextElement();
	
			
			model.setValueAt(String.valueOf(contTable),contTable,0);
			model.setValueAt(att,contTable, 1);
			contTable ++;
		}
	}
	
	/**
	 * Inicializa el contenido de la informacion del panel de un atributo seleccionado
	 * */
	protected void clearAttributeInfo() {
		jAttributeNameLabel1.setText("");
		jMissingLabel.setText("");
		jDistinctLabel.setText("");
		jUniqueLabel.setText("");
		
	}
	
	private void clearFitersAdded(){
		
		jFilterAddedList.removeAll();
		jFilterAddedList.repaint();					
		//listDataFiltros.removeAllElements();
		
		InstanceManager.eINSTANCE.removeAllFiltersOfRelation();
		
	}
	
	/**
	 * Metodo para visualizar la informacion de la relacion en la pantalla de pre-proceso
	 * 
	 * @param nameRelation 
	 * @param numInstance
	 * @param numAttribute
	 * 
	 * */
	public void updateRelationInfo(String nameRelation,int numInstances, int numAttributes){
		jRelationNameLabel.setText(nameRelation);
		jInstancesLabel.setText(String.valueOf(numInstances));
		jAttributesLabel.setText(String.valueOf(numAttributes));
	}
	
	/**
	 * Metodo para visualizar la informacion de un atributo en la pantalla de pre-proceso
	 * 
	 * @param attributes Attribute
	 * @param stats AttributeStats 
	 * 
	 * */
	public void updateAttributeInfo(Attribute attribute,AttributeStats stats){
		
		jAttributeNameLabel1.setText(attribute.name());
		jMissingLabel.setText(String.valueOf(stats.missingCount));
		jDistinctLabel.setText(String.valueOf(stats.distinctCount));
		jUniqueLabel.setText(String.valueOf(stats.uniqueCount));
	}
	/**
	 * Metodo para visualizar la informacion de un atributo en la pantalla de pre-proceso, 
	 * para los casos que no se haya cargado un arff inicialmente
	 * 
	 * @param nameAtt String
	 * 
	 * */
	public void updateAttributeInfo(String nameAtt){
		
		jAttributeNameLabel1.setText(nameAtt);
		jMissingLabel.setText("-1");
		jDistinctLabel.setText("-1");
		jUniqueLabel.setText("-1");
	}
	
	/**
	 * This method initializes jChooseScrollPane1	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJChooseScrollPane1() {
		if (jChooseScrollPane1 == null) {
			jChooseScrollPane1 = new JScrollPane();
			jChooseScrollPane1.setEnabled(true);
			jChooseScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.blue,1));
			jChooseScrollPane1.setViewportView(getJFilterTree1());
			jChooseScrollPane1.setVisible(false);
		}
		return jChooseScrollPane1;
	}
	
	/**
	 * This method initializes jFilterTree1	
	 * 	
	 * @return javax.swing.JTree	
	 */
	private JTree getJFilterTree1() {
		if (jFilterTree1 == null) {
			Vector<Filter> filters = InstanceManager.eINSTANCE.allFilters();
			DefaultMutableTreeNode raiz = new DefaultMutableTreeNode("Filters");
//			DefaultMutableTreeNode supervised = new DefaultMutableTreeNode("Supervised");
//			DefaultMutableTreeNode unsupersived = new DefaultMutableTreeNode("Unsupersived");
//			DefaultMutableTreeNode attributeSupervised = new DefaultMutableTreeNode("Attribute");
//			DefaultMutableTreeNode instanceSupervised = new DefaultMutableTreeNode("Instance");
//			DefaultMutableTreeNode attributeUnsupersived = new DefaultMutableTreeNode("Attribute");
//			DefaultMutableTreeNode instanceUnsupersived = new DefaultMutableTreeNode("Instance");
			//Agregado de filtros
			for (int i = 0; i < filters.size(); i++) {
				Filter filter = filters.get(i);
				DefaultMutableTreeNode nodo = new DefaultMutableTreeNode(filter.getName());
				raiz.add(nodo);
//				if(filter.isSupervisedFilter() && filter.isAttributeFilter()){
//				attributeSupervised.add(nodo);
//				}else if(filter.isSupervisedFilter() && !filter.isAttributeFilter()){
//				instanceSupervised.add(nodo);
//				}else if(!filter.isSupervisedFilter() && filter.isAttributeFilter()){
//				attributeUnsupersived.add(nodo);
//				}else if(!filter.isSupervisedFilter() && !filter.isAttributeFilter()){
//				instanceUnsupersived.add(nodo);
//				}	
			}
			
			
			//Agregado de Capetas
//			supervised.add(attributeSupervised);
//			supervised.add(instanceSupervised);
//			unsupersived.add(attributeUnsupersived);
//			unsupersived.add(instanceUnsupersived);
//			raiz.add(supervised);
//			raiz.add(unsupersived);
			
			
			
			jFilterTree1 = new JTree(raiz);
			jFilterTree1
			.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
				public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
					DefaultMutableTreeNode selection = (DefaultMutableTreeNode)jFilterTree1.getLastSelectedPathComponent();
					if(InstanceManager.eINSTANCE.isFilter(selection.toString())){
						getJFilterTextField().setText(selection.toString());
						//Se acomoda la interface
						setEnabled(true);
						getJChooseScrollPane1().setVisible(false);
						getJFilterTree1().setVisible(false);						
						getJFilterWindow().dispose();
//						la siguiente linea es para que quede selccionada la raiz
						getJFilterTree1().setSelectionRow(0);
						UserProfilingUI.eINSTANCE.setEnabled(true);
						
					}
				}
			});
		}
		jFilterTree1.setSelectionRow(0);
		return jFilterTree1;
	}
	
	public JWindow getJFilterWindow() {
		if(jFilterWindow == null)
			jFilterWindow = new JWindow();
		jFilterWindow.setSize(new java.awt.Dimension(150,200));
		jFilterWindow.setLocation(getJChooseButton().getLocationOnScreen());
		jFilterWindow.setContentPane(getJChooseScrollPane1());
		
		
		return jFilterWindow;
	}


	/**
	 * This method initializes jFilterAddedScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJFilterAddedScrollPane() {
		if (jFilterAddedScrollPane == null) {
			jFilterAddedScrollPane = new JScrollPane();
			jFilterAddedScrollPane.setBounds(new java.awt.Rectangle(5,14,373,138));
			jFilterAddedScrollPane.setViewportView(getJFilterAddedList());
		}
		return jFilterAddedScrollPane;
	}
	
	
	
}
